#pragma once

namespace gg_bond
{
	template<class T>
	class vector
	{
		typedef T* iterator;
		typedef const T* const_iterator;
	publlic:
		vector()
			: _start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{

		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._endof_storage);
		}

		vector(const vector<T>& v)
			: _start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			vector<T> tmp(v.begin(), v.end());
			swap(tmp);
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		vector<string> v;

		void resize(size_t n,const vector<T>& val = T())
		{
			if (n > capacity())
			{
				reserve(n);
			}

			if (n > size)
			{
				while (_finish < _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
		}

		T& front()
		{
			assert(size());

			return *_start;
		}

		T& back()
		{
			assert(size());
			return *(_finish - 1);
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const iterator begin() const
		{
			return _start;
		}
		const iterator end() const
		{
			return _finish;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, sizof(T) *sz);
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + capacity();
			}
		}

		void push_back(T& x)
		{
			/*if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;*/
			insert(end(), x);
		}

		void pop_back()
		{
			assert(_finish > _start);
			--_finish;
		}

		void insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}

			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}
			*pos = x;

			++_finish;
		}

		 iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator begin = pos + 1;
			while (begin < _finish)
			{
				*(begin - 1) = *begin;
				++begin;
			}

			--finish;

			return pos;
		}

		 T& operator[](size_t pos)
		 {
			 assert(pos < size());

			 return _start[pos];
		 }


	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storage;
	};

}
